package Entities;

import java.util.ArrayList;
import Entities.Order_item;
import Entities.Stock;
import digitshoplogic.DBAccessSingleton;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Stock_item {
	public int UPC;
        public int categoryId;
	public String name;
	public BigDecimal retailPrice;
	public BigDecimal taxRate;
	public BigDecimal cost;
        public String imageURL;

        public static final Stock_item GetStockItemByUPC(int UPC) throws SQLException{
        Stock_item  ret = new Stock_item();
        String upcStr = Integer.toString(UPC);
        Connection con = DBAccessSingleton.getInstance().getConnection();
        String queryStr = "select * from stock_item where stock_item.`UPC`= "+ upcStr;
        ResultSet rs = DBAccessSingleton.getInstance().getStatement().executeQuery(queryStr);

         while (rs.next()) {
                ret.UPC = rs.getInt(1);
                ret.categoryId = rs.getInt(2);
                ret.name = rs.getString(3);
                ret.retailPrice = rs.getBigDecimal(4);
                ret.taxRate = rs.getBigDecimal(5);
                ret.cost = rs.getBigDecimal(6);
                ret.imageURL = rs.getString(7);
            }
            rs.close();
        return ret;
    }
    public static void UpdateStockItemByObj(Stock_item item) throws SQLException{

        String updateStr = "";
        int upc = item.UPC;
        String upcStr = Integer.toString(upc);

        int categoryId = item.categoryId;
        String categoryIdStr = Integer.toString(categoryId);

        String name = item.name;

        BigDecimal retailPrice = item.retailPrice;
        String retailPriceStr = retailPrice.toString();

        BigDecimal taxRate = item.taxRate;
        String taxRateStr = taxRate.toString();

        BigDecimal cost = item.cost;
        String costStr = cost.toString();

        String imageURL = item.imageURL;


        updateStr = "INSERT INTO `shop_db`.`stock_item` (`categoryId`, `name`, `retailPrice`, `taxRate`, `cost`, `imageURL`) VALUES ("+ categoryIdStr +", '" + name + "', '" + retailPriceStr +"', '"+taxRateStr+"', '" + costStr + "', '"+ imageURL+"');";

       Statement stm = DBAccessSingleton.getInstance().getStatement();
       stm.execute(updateStr);
    }

        public static void AddStockItemByObj(Stock_item item) throws SQLException{

        String updateStr = "";
        int upc = item.UPC;
        String upcStr = Integer.toString(upc);

        int categoryId = item.categoryId;
        String categoryIdStr = Integer.toString(categoryId);

        String name = item.name;

        BigDecimal retailPrice = item.retailPrice;
        String retailPriceStr = retailPrice.toString();

        BigDecimal taxRate = item.taxRate;
        String taxRateStr = taxRate.toString();

        BigDecimal cost = item.cost;
        String costStr = cost.toString();

        String imageURL = item.imageURL;


        updateStr = "INSERT INTO `shop_db`.`stock_item` (`categoryId`, `name`, `retailPrice`, `taxRate`, `cost`, `imageURL`) VALUES ("+ categoryIdStr +", '" + name + "', '" + retailPriceStr +"', '"+taxRateStr+"', '" + costStr + "', '"+ imageURL+"');";

       Statement stm = DBAccessSingleton.getInstance().getStatement();
       stm.execute(updateStr);
    }

        public void outputString(){
            System.out.print("UPC:          "); System.out.println(UPC);
            System.out.print("categoryId:   "); System.out.println(categoryId);
            System.out.print("name :        "); System.out.println(name);
            System.out.print("retailPrice:  "); System.out.println(retailPrice);
            System.out.print("taxRate:      "); System.out.println(taxRate);
            System.out.print("cost:         "); System.out.println(cost);
            System.out.print("imageURL:     "); System.out.println(imageURL);
        }

        public void setUPC(int UPC){
            this.UPC = UPC;
        }
        public void setCategoryID(int categoryId){
            this.categoryId = categoryId;
        }
	public void setName(String name){
            this.name = name;
        }
	public void setRetailPrice(BigDecimal retailPrice){
            this.retailPrice = retailPrice;
        }
	public void setTaxRate (BigDecimal taxRate){
            this.taxRate = taxRate;
        }
	public void setCost (BigDecimal cost){
            this.cost = cost;
        }
        public void setImageURL(String imageURL){
            this.imageURL = imageURL;
        }

        public int getUPC(){
            return this.UPC;
        }
        public int getCategoryID(){
            return this.categoryId;
        }
	public String getName(){
            return this.name;
        }
	public BigDecimal getRetailPrice(){
            return this.retailPrice;
        }
	public BigDecimal getTaxRate (){
            return this.taxRate;
        }
	public BigDecimal getCost (){
            return this.cost;
        }
        public String getImageURL(){
            return this.imageURL;
        }


}